//
//  ImageControlM.m
//  
//  jhf
//
//  Created by Mac on 2020/7/31.
//

#import "ImageControlM.h"
#import <CooperSDK/CooperSDK.h>
#import <ReactiveObjC/ReactiveObjC.h>

@implementation ImageControlM

- (CGFloat)calCellHeight:(CGSize)size {
    if (self.isScale) { // 缩放+最大宽度或者高度计算
        CGFloat scale = size.height / size.width;
        CGFloat lastHeight = 0;
        CGFloat totalWidth = (self.containerWidthCons > 0 ? self.containerWidthCons : kScreen_Width);
        if (self.vMaxHeightCons > 0 && size.height > self.vMaxHeightCons) {
            lastHeight = self.vMaxHeightCons;
            if ((lastHeight / scale) > totalWidth) { // 宽度超了
                lastHeight = totalWidth * scale;
            }
        }else if (self.vMaxWidthCons > 0 && size.width > self.vMaxWidthCons) {
            lastHeight = self.vMaxWidthCons * scale;
        }else {
            lastHeight = size.height;
        }
        CGFloat lastWidth = lastHeight / scale;
        self.paddingInsets = UIEdgeInsetsMake(0, 0, 0,  totalWidth - self.vLeftCons - self.vRightCons - lastWidth);
        return lastHeight;
    }
    if (self.vWidthCons > 0 && self.vHeightCons > 0) { // 固定大小
        return self.vHeightCons;
    }
    CGFloat contentWidth = (kScreen_Width - self.paddingInsets.left - self.paddingInsets.right) - self.vLeftCons - self.vRightCons; // 铺满 宽度
    CGFloat contentHeight = 0.f;
    if (size.width < contentWidth && self.isReal) { //真实图片宽度小于预计宽度
        contentHeight = size.height;
    }else
        if (size.width > 0) {
            contentHeight =  (size.height / size.width) * contentWidth;
        }
    return contentHeight;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self defaultConfig];
    }
    return self;
}

- (void)defaultConfig {
    self.paddingInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    self.contentMode = UIViewContentModeScaleAspectFill;
}

+ (ImageControlM *)defaultIMGSource:(UIImage *)imageSource; {
    ImageControlM *model = [[ImageControlM alloc] init];
    model.imageSource = imageSource;
    return model;
}

+ (ImageControlM *)defaultIMGUrl:(NSString *)url; {
    ImageControlM *model = [[ImageControlM alloc] init];
    model.imageUrl = url;
    return model;
}

+ (ImageControlM *)defaultIMGName:(NSString *)name; {
    ImageControlM *model = [[ImageControlM alloc] init];
    model.imageName = name;
    return model;
}

- (CGFloat)cellHeight; {
    /// 除了高度的其它边距
    CGFloat paddingHeight = self.paddingInsets.top + self.paddingInsets.bottom + self.vTopCons + self.vBottomCons;
    return paddingHeight + self.vHeightCons;
}

- (void)controlIMG:(UIImageView *)imgV blockBool:(BlockBool)block {
    [self controlV:imgV];
    [imgV setContentMode:self.contentMode];
    if (self.imageSource || self.imageName) {
        UIImage *image = self.imageSource;
        if (self.imageName) {
            image = [UIImage imageNamed:self.imageName];
        }
        if (image && image.size.width > 0 && image.size.height > 0) {
            // 计算图片的大小
            if (self.vHeightCons <= 0) {
                self.vHeightCons = [self calCellHeight:image.size];
            }
            // 模糊
            if (self.isBlur) {
                [imgV setImage:[JUtil blurryImage:image withBlurLevel:0.2]];
            }
            // 图片真实大小
            self.imageHeight = image.size.height;
            self.imageWidth = image.size.height;
            [imgV setImage:image];
            // block返回结果
            if (block) {
                block(true);
            }
        }else {
            if (self.imagePlaceSource) {
                [imgV setImage:self.imagePlaceSource];
            }else
            if (self.imagePlaceholder) {
                [imgV setImage:[UIImage imageNamed:self.imagePlaceholder]];
            }
            if (block) {
                block(false);
            }
        }
    }else if (self.imageUrl) {
        @weakify(self);
        UIImage *placeHoder = self.imagePlaceSource;
        if (self.imagePlaceholder) {
            placeHoder = [UIImage imageNamed:self.imagePlaceholder];
        }
        [imgV sd_setImageWithURL:cp_imageUrl(self.imageUrl) placeholderImage:placeHoder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image && image.size.width > 0 && image.size.height > 0) {
                @strongify(self);
                // 计算图片的大小
                if (self.vHeightCons <= 0) {
                    self.vHeightCons = [self calCellHeight:image.size];
                }
                // 模糊
                if (self.isBlur) {
                    [imgV setImage:[JUtil blurryImage:image withBlurLevel:0.2]];
                }
                // 图片真实大小
                self.imageHeight = image.size.height;
                self.imageWidth = image.size.width;
                // block返回结果
                if (block) {
                    block(true);
                }
            }else {
                if (block) {
                    block(false);
                }
            }
        }];
        
    }else {
        UIImage *placeHoder = self.imagePlaceSource;
        if (self.imagePlaceholder) {
            placeHoder = [UIImage imageNamed:self.imagePlaceholder];
        }
        if (placeHoder) {
            [imgV setImage:placeHoder];
        }
    }
}

@end
